package zozopo.user;

import java.util.Map;


import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import zozopo.comm.Validate;
import zozopo.log.operations.UserLoginLogOS;
import zozopo.log.operations.UserLoginLogTB;
import zozopo.user.operations.TokenOS;
import zozopo.user.operations.TokenTB;
import zozopo.user.operations.UserOS;
import zozopo.user.operations.UserTB;

import com.octo.captcha.module.servlet.image.SimpleImageCaptchaServlet;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport implements Action ,SessionAware, ServletResponseAware,ServletRequestAware{

	private HttpServletResponse response;
	private HttpServletRequest request;
	private Map<Object, Object> session;
	
	private String redirect;
	
	private String account;
	private String password;
	private String valicode;
	
	//输出参数
	private UserTB user;
	
	public String execute() throws Exception {
	

		return "success";
	}
	
	public void validate() {
		
		ActionContext actionContext = ActionContext.getContext(); 
		
	
		
		//验证码
		boolean captchaPassed = SimpleImageCaptchaServlet.validateResponse(request, valicode);
		
		if(account.trim().equals("")){
			addFieldError("errormessage", getText("background.login.username") + getText("background.common.isnotnull"));
			return;
		}
		
		
		
		
		UserOS userOS = new UserOS();
		
		TokenOS tokenOS = new TokenOS(userOS.getConn());
		UserLoginLogOS userLoginLogOS = new UserLoginLogOS(userOS.getConn());
		
		
		
		try{
		
			TokenTB token = new TokenTB();
			
			//日志
			UserLoginLogTB userLoginLog = new UserLoginLogTB();
			userLoginLog.setType("界面");
			userLoginLog.setAccount(account);
			userLoginLog.setIp(request.getRemoteAddr());
			
			
			if(!userLoginLogOS.isAccountSecurity(account)){// 如果超出输入密码错误次数.账号存在异常.
				addFieldError("errormessage","账号异常,存在安全问题.请稍后再输入");
				return;
			}
			
			if(userLoginLogOS.isWorngOutOfTime(account)){// 如果超出输入密码错误次数.需要校验证码.
				if(!captchaPassed){
					addFieldError("errormessage",getText("background.login.valicode.iswrong"));
					return;
				}
			}
			
			
			if(Validate.isEmail(account)){
	
			
				if(account.length() > 320){
					userOS.releaseConnection();
					addFieldError("errormessage","email大于320个字符,请重新输入");
					return;
				}
				
				if(!userOS.isRealUserByEmail(account, password)){
					
					
					//写入错误的登陆日志
					userLoginLog.setIswrong(true);
					userLoginLogOS.insert(userLoginLog);
					
					//清空session valicode
					userOS.releaseConnection();
					
					
					actionContext.getSession().put("valicode", "");
					addFieldError("errormessage", getText("background.login.sign.wrong"));
					return;
				}
				
			}else{
				if(!userOS.isRealUser(account, password)){
					//写入错误的登陆日志
					userLoginLog.setIswrong(true);
					userLoginLogOS.insert(userLoginLog);
					
					
					//清空session valicode
					userOS.releaseConnection();
					actionContext.getSession().put("valicode", "");
					addFieldError("errormessage", getText("background.login.sign.wrong"));
					return;
				}
				
				
				
				
			}
			
			//获取上次次登陆信息
			
			//写入登陆日志
			userLoginLog.setIswrong(false);
			userLoginLogOS.insert(userLoginLog);
			
			UserLoginLogTB  lastLoginLog= userLoginLogOS.getNewLoginInfo(account);
			
			user = userOS.getUserInfoByName(account);
			
			session.put("userInfo", user);
			session.put("loginLog", lastLoginLog);
			
			
			
			String tokenStr = request.getSession().getId();
			token.setOpenid(user.getId_user()+"");
			token.setToken(tokenStr);
			token.setUsername(user.getName_user());
			
			
			Cookie cookie = new Cookie("SESSIONID", tokenStr);
			cookie.setMaxAge(24*60*60*90);   //秒
			response.addCookie(cookie);
			
			tokenOS.deleteByOpenid(user.getId_user()+"");//清空
			tokenOS.insert(token);
		}finally{
			userOS.releaseConnection();
		}

	}

	
	

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	

	public String getValicode() {
		return valicode;
	}

	public void setValicode(String valicode) {
		this.valicode = valicode;
	}

	public void setSession(Map arg0) {
		// TODO Auto-generated method stub
		session = arg0;
	}
	public void setServletResponse(HttpServletResponse arg0) {
		// TODO Auto-generated method stub
		response = arg0;
	}
	
	public String getRedirect() {
		return redirect;
	}

	public void setRedirect(String redirect) {
		
		this.redirect = redirect;
	}

	

	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		request = arg0;
	}

	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}
}
